﻿using Autodesk.Revit.UI;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ZhuXingFamilys.ExportExcel.entity;

namespace ZhuXingFamilys.ExportExcel
{
    /// <summary>
    /// SelectSchema.xaml 的交互逻辑
    /// </summary>
    public partial class SelectSchema : Window
    {
        UIApplication application;
        IList<SchemaName> schemaNameList;
        public SelectSchema(UIApplication uiApp, IList<SchemaName> schemaNameList)
        {
            this.application = uiApp;
            this.schemaNameList = schemaNameList;
            //ui库使用
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (schemaNameList != null)
            {
                ListViewForName.ItemsSource = schemaNameList.ToList();
            }
        }
        // 加载
        private void ok_Click(object sender, RoutedEventArgs e)
        {
            if (ListViewForName.SelectedValue != null)
            {
                SchemaName sn = ListViewForName.SelectedItem as SchemaName;
                ExportToExcel exporttoexcel = ExportToExcel.GetInstance(application);
                exporttoexcel.loadExcel(sn);
            }
        }

        private void WindowOnClosed(object sender, EventArgs e)
        {
            ExportToExcel exporttoexcel = ExportToExcel.GetInstance(application);
            exporttoexcel.SelectSchema_Closed();
        }
        // 双击
        private void ListView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ListViewForName.SelectedValue != null)
            {
                SchemaName sn = ListViewForName.SelectedItem as SchemaName;
                ExportToExcel exporttoexcel = ExportToExcel.GetInstance(application);
                exporttoexcel.loadExcel(sn);
            }
        }
        // 删除逻辑
        // 删除存储
        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (ListViewForName.SelectedValue != null)
            {
                SchemaName sn = ListViewForName.SelectedItem as SchemaName;
                ExportToExcel exporttoexcel = ExportToExcel.GetInstance(application);
                exporttoexcel.deleteExcel(sn);
                deleteFromList(sn);
                //MessageBoxResult re = MessageBox.Show("此操作将删除已保存的明细表数据，确定吗？", "提示", MessageBoxButton.OKCancel);
                //if (re == MessageBoxResult.OK)
                //            {
                //	SchemaName sn = ListViewForName.SelectedItem as SchemaName;
                //	ExportToExcel exporttoexcel = ExportToExcel.GetInstance(commandData);
                //	exporttoexcel.deleteExcel(sn);
                //                deleteFromList(sn);
                //}
            }
        }

        // 删除本地
        private void deleteFromList(SchemaName schemaName)
        {
            foreach (var item in schemaNameList)
            {
                if (item.Name.Equals(schemaName.Name))
                {
                    schemaNameList.Remove(item);
                    break;
                }
            }
            ListViewForName.ItemsSource = schemaNameList.ToList();
        }
        // 刷新逻辑
        public void reloadList(IList<SchemaName> _schemaNameList)
        {
            schemaNameList = _schemaNameList;
            ListViewForName.ItemsSource = schemaNameList.ToList();
        }
    }
}
